var http = require('../../utils/http.js')
var util = require('../../utils/util.js')
var localstorage = require('../../utils/localstorage.js');
var appInstance = getApp()



Page({

  /**
   * 页面的初始数据
   */
  data: {

    searchWork: "",
    searchHistory: [],
    hots: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      searchHistory: wx.getStorageSync('searchWordd') || []
    })
    this.getHotist();
  },

  getHotist() {
    var _this = this;
    var postdata = {
      "Page": 1,
      "PageSize": 5,
    }
    http.request({
      url: 'car_searchkeyServiceApi/page',
      method: "Post",
      data: postdata,
      callBack: (res) => {
        // console.log(res)
        let newArr = res.result.items;
        let totalPages = res.result.totalPages

        wx.stopPullDownRefresh()

        _this.setData({
          hots: newArr,
        })


      }
    })
  },



  onChange(e) {

    this.setData({
      searchWork: e.detail,
    });

  },

  onSearch() {

    const searchHistory = wx.getStorageSync('searchWordd') || []
    searchHistory.unshift(this.data.searchWork)
    wx.setStorageSync('searchWordd', searchHistory)
    this.setData({
      searchHistory: searchHistory,
    })


    wx.navigateTo({
      url: './search2?words=' + this.data.searchWork,
    })

  },

  onclickitem(e) {

    var item = e.currentTarget.dataset.item;
    //  console.log(item)
    this.setData({
      searchWork: item,
    });
    // this.getdatalist(item)
    wx.navigateTo({
      url: './search2?words=' + item,
    })
  },

  onclickitemHot(e) {

    var item = e.currentTarget.dataset.item;

    const searchHistory = wx.getStorageSync('searchWordd') || []
    searchHistory.unshift(item.words)
    wx.setStorageSync('searchWordd', searchHistory)
    this.setData({
      searchHistory: searchHistory,
    })


    wx.navigateTo({
      url: './search2?words=' + item.words,
    })
  },

  onclickHistory() {
    var _this = this
    wx.showModal({
      title: '提示',
      content: '是否清楚本地搜索记录？',
      success(res) {
        if (res.confirm) {
          wx.setStorageSync('searchWordd', [])
          _this.setData({
            searchHistory: [],
          })

        } else if (res.cancel) {

        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  //  onReachBottom() {
  //   if (this.data.isMore) {
  //     this.setData({
  //       isloading: true
  //     })
  //     page = page + 1;
  //     this.getdatalist(
  //       this.data.searchWork
  //     );
  //   }
  // },

  // onPullDownRefresh() {

  //   if(this.data.searchWork.length==0)
  //   {
  //        return;      
  //   }
  //   this.setData({
  //     datalist: [],
  //   })
  //   page = 1;
  //   this.getdatalist(
  //     this.data.searchWork
  //   );

  // },






  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})